package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.service.IPropertySign;
import com.logic.landseaserver.ws.dto.PropertySignDTO;
import com.logic.landseaserver.ws.request.PropertySignEstimationReq;
import com.logic.landseaserver.ws.request.PropertySignReq;
import com.logic.landseaserver.ws.response.SignEstimationListResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Quintina on 2017/4/25.
 */
@Path("/propertySign")
@Api(value = "物业签订")
public class PropertySignRest {

    @Autowired
    private IPropertySign propertySignService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业签订上传合同", position = 1)
    public Response insert(@ApiParam(value = "param", required = true) PropertySignReq propertySignReq) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertySignService.insert(propertySignReq));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("insert error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("insert error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "签订合同列表", position = 2)
    public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<PropertySignDTO> projectDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            projectDTOList = (List<PropertySignDTO>) propertySignService.queryList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(projectDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("list error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业签订删除", position = 3)
    public Response delete(@ApiParam(value = "param", required = true) @QueryParam("ids") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertySignService.delete(ids);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("delete error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("delete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/signEstimation")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "物业审批添加记录", position = 1)
    public Response signEstimation(@ApiParam(value = "param", required = true) PropertySignEstimationReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(propertySignService.signEstimation(req));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("signEstimation error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("signEstimation error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/signEstimationPage")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "签订评估列表", position = 1)
    public Response signEstimationPage(@ApiParam(value = "param", required = true)QueryUtil util) {
        CommonResultMap map = new CommonResultMap();
        List<SignEstimationListResp> projectDTOList = null;
        try {
            util.getPagingTool().calculateStartIndex();
            projectDTOList = (List<SignEstimationListResp>) propertySignService.signEstimationPage(util);
            Integer totalCount = util.getPagingTool().getTotalNum();
            map.setData(projectDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        }  catch (Exception e) {
            logger.error("signEstimationPage error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
